<template>
  <div class="app-container">
    <!-- 添加或修改报修设置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="960px" append-to-body v-drag-fullscreen>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="8" :xs="24">
            <el-form-item label="类型名称" prop="manageName">
              <el-input v-model="form.manageName" placeholder="请输入类型名称" :disabled="disabled"/>
            </el-form-item>
          </el-col>
          <el-col :span="8" :xs="24">
            <el-form-item label="报修类型" prop="manageTypes">
              <el-select v-model="form.manageTypes" placeholder="请选择报修设置类型" :disabled="disabled">
                <el-option
                  v-for="item in dict.manage_types"
                  :key="item.dictValue"
                  :label="item.dictLabel"
                  :value="item.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8" :xs="24">
            <el-form-item label="公共区域" prop="isPublic">
              <el-select v-model="form.isPublic" placeholder="请选择公共区域" :disabled="disabled">
                <el-option
                  v-for="item in dict.sys_yes_no"
                  :key="item.dictValue"
                  :label="item.dictLabel"
                  :value="item.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="16" :xs="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" :disabled="disabled"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { getRepairManage , addRepairManage, updateRepairManage } from "@/api/waitress/RepairManage";

  export default {
    name: "RepairManageFrom",
    dicts: [   'manage_types',  'sys_yes_no',   ],
    data() {
      return {
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        //是否禁用
        disabled: false,
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          manageName: [
            { required: true, message: "类型名称不能为空", trigger: "blur" }
          ],
          manageTypes: [
            { required: true, message: "报修类型不能为空", trigger: "blur" }
          ],
        }
      };
    },
    created() {

    },
    methods: {
      show(option){
        this.reset();
        this.disabled = option.type == 'VIEW'
        if(option.type=='ADD'){
          this.open = true;
          this.title = "新增报修设置";
        }else if(option.type == 'UPDATE' || option.type == 'VIEW'){
          getRepairManage(option.id).then(response => {
            this.form = response.data;

            if(option.type == 'VIEW'){
              this.title = "查看报修设置";
            }else{
              this.title = "修改报修设置";
            }
            this.open = true;
          });
        }
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
        this.$emit("cancel")
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          manageName: null,
          manageTypes: null,
          isPublic: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null
        };
        this.resetForm("form");
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateRepairManage(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.cancel()
              });
            } else {
              addRepairManage(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.cancel()
              });
            }
          }
        });
      }
    }
  };
</script>
